﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
namespace Server
{
    public class CGui_NhanDuLieu
    {
        #region "Biến"
        StreamReader _reader;

        public StreamReader Reader
        {
            get { return _reader; }
            set { _reader = value; }
        }
        StreamWriter _writer;

        public StreamWriter Writer
        {
            get { return _writer; }
            set { _writer = value; }
        }

        public delegate void DongKetNoiHandler(TcpClient client,  EventArgs e);
        public event DongKetNoiHandler DongKetNoiEvent;
        #endregion

        #region "Hàm"
        /// <summary>
        /// Khởi tạo
        /// </summary>
        /// <param name="client"></param>
        public CGui_NhanDuLieu(TcpClient client)
        {
            _reader = new StreamReader(client.GetStream());
            _writer = new StreamWriter(client.GetStream());
        }
        /// <summary>
        /// Gửi dữ liệu
        /// </summary>
        public bool GuiDuLieu(string msg, TcpClient client)
        {

            if (client.Connected == true)
            {
                string mahoa = CCryptography.MaHoa(msg);
                try
                {
                    _writer.WriteLine(mahoa);
                    _writer.Flush();
                    if (GuiDuLieuEvent != null)
                    {
                        GuiDuLieuEvent(mahoa, client);
                    }
                }
                catch (Exception c)
                {
                    if (DongKetNoiEvent != null)
                    {
                        DongKetNoiEvent(client, new EventArgs());
                    }
                    //throw new Exception("Không thể gửi dữ liệu tới " + client.Client.LocalEndPoint.ToString());
                }
            }
            else
            {
                return false;
            }
            return true;

        }
        /// <summary>
        /// Nhận dữ liệu
        /// </summary>
        public string NhanDuLieu(TcpClient client)
        {
            string mahoa = "";

            try
            {
                mahoa = _reader.ReadLine();
                if (NhanDuLieuEvent != null)
                {
                    NhanDuLieuEvent(mahoa, client);
                }
            }
            catch (Exception c)
            {
                if(DongKetNoiEvent!= null)
                {
                    DongKetNoiEvent(client, new EventArgs());
                }
                //throw new Exception("Không thể nhận dữ liệu từ " + client.Client.LocalEndPoint.ToString());
                return "";
            }
            string msg = CCryptography.GiaiMa(mahoa);

            return msg;
        }
        #endregion

        #region "Sự kiện"
        public delegate void NhanDuLieuHandler(string msg, TcpClient client);
        public delegate void GuiDuLieuHander(string msg, TcpClient client);

        public event NhanDuLieuHandler NhanDuLieuEvent;
        public event GuiDuLieuHander GuiDuLieuEvent;
        #endregion

    }
}
